<template>
  <div class="topSelect">
    <div class="topSelectLabel">
      <div class="name">类 别</div>
    </div>
    <div class="topSelectList">
      <!-- 这个checkbox用于计算元素宽度 -->
      <el-checkbox-group v-model="selectedClass" id="dis">
        <el-checkbox v-for="item in classList" :key="item.careerTagId" class="topSelectItem"
          :label="item">{{ item.tagTitle }}</el-checkbox>
      </el-checkbox-group>
      <span @click="handleCheckAllChange" class="topSelectItem selectAll" :style="{
          color: checkAll ? '#0B6EFF' : '',
          background: checkAll ? '#CADFFF' : '',
        }">全部</span>
      <el-checkbox-group v-model="selectedClass" @change="handleCheckedClassChange">
        <el-checkbox v-for="item in currentClassList" :key="item.careerTagId" class="topSelectItem"
          :label="item">{{ item.tagTitle }}</el-checkbox>
      </el-checkbox-group>
      <div class="topSelectMore" v-if="btnShow" @click="partChange">
        <button v-if="part">更多<i class="el-icon-arrow-down"></i></button>
        <button v-else>收起<i class="el-icon-arrow-up"></i></button>
      </div>
    </div>
  </div>
</template>
<script>
import { positionTreeInterface } from "@/api/home.js";

export default {
  data() {
    return {
      classList: [],
      selectedClass: [],
      part: true,
      currentClassList: [],
      partData: [],
      btnShow: false,
      isIndeterminate: false,
      checkAll: true,
    };
  },
  methods: {
    getClassList() {
      //console.log("类别");
      positionTreeInterface({
        careerTagIds: "",
      }).then((res) => {
        //console.log("所有一级类", res);
        if (res.code == 200) {
          this.classList = res.data;
          this.btnShow = true;
          //获取数据后在渲染筛选类别列表
          this.filterListWidth();
          this.$emit("selectedChecks", this.classList);
        }
      });
    },
    handleCheckedClassChange(value) {
      let checkedCount = value.length;
      this.checkAll = false;
      // this.checkAll = checkedCount === this.classList.length;
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.classList.length;
      if (checkedCount > 0) {
        this.$emit("selectedChecks", value);
      } else {
        this.checkAll = true;
        let arr = [];
        for (let item of this.classList) {
          arr.push(item);
        }
        this.$emit("selectedChecks", arr);
      }
    },
    handleCheckAllChange() {
      this.checkAll = !this.checkAll;
      let arr = [];
      for (let item of this.classList) {
        arr.push(item);
      }
      //全部按钮与其他冲突
      this.selectedClass = [];
      this.$emit("selectedChecks", this.checkAll ? arr : []);

      // this.selectedClass = value ? arr : [];
      // this.$emit("selectedChecks", this.selectedClass);
      this.isIndeterminate = false;
    },
    partChange() {
      if (this.part) {
        this.part = false;
        this.currentClassList = this.classList;
      } else {
        //展开
        this.part = true;
        this.currentClassList = this.partData;
      }
    },
    filterListWidth() {
      setTimeout(() => {
        const dis = document.getElementById("dis");
        let wid1 = 0;
        let wid2 = 0;

        const el = document.getElementsByClassName("el-checkbox-group")[0];
        const boxWidth = Number(
          window.getComputedStyle(el).width.replace("px", "")
        );
        try {
          dis.childNodes.forEach((item, idx) => {
            wid1 += item.clientWidth + 20;
            if (boxWidth < wid1 + 72) {
              wid2 += item.clientWidth + 20;
            }
            if (boxWidth >= wid2) {
              this.partData.push(this.classList[idx]);
              this.currentClassList = this.partData;
            } else {
              throw new Error();
            }
          });
        } catch {
        } finally {
          if (boxWidth >= wid2) {
            this.btnShow = false;
          }
          dis.className = "disn";
          let arr = [];
          for (let item of this.classList) {
            arr.push(item);
          }
          // this.selectedClass = arr;
        }
      }, 100);
    },
  },
  mounted() {
    this.getClassList();

    // setTimeout(() => {
    // }, 2000);
  },
};
</script>
<style lang="scss" scoped>
.disn {
  display: none;
}
.topSelect {
  // height:108px;
  padding: 7px;
  padding-right: 70px;
  background: linear-gradient(180deg, #b7d4ff 0%, #f1f4f9 50%, #f1f4f9 100%);
  border-radius: 18px;
  margin-top: 18px;
  min-height: 80px;
  display: flex;
  margin: 14px;
  margin-top: 12px;
  position: sticky;
  top: 60px;
  // box-shadow: 0 10px 20px rgba(0, 0, 0, 0.26);
  z-index: 20;
  width: 98%;
  .topSelectLabel {
    flex: 0 0 80px;
    font-size: 17px;
    display: flex;
    margin: 10px 0 15px 15px;
    align-items: center;
    justify-content: center;
    border-right: 2px solid #c8d4e6;
    border-radius: 1px;
    padding-right: 15px;
    .name {
      position: relative;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 600;
      color: #000000;
      line-height: 20px;
      letter-spacing: 5px;
    }
    .name::before {
      content: "";
      z-index: -1;
      position: absolute;
      bottom: -12px;
      left: 50%;
      width: 50px;
      height: 40px;
      border-radius: 50% / 100% 100% 0 0;
      background: linear-gradient(
        to bottom,
        #0b6eff,
        #87b6fb 10%，#cee0fa 22%,
        #f1f4f9 25%
      );
      transform: skewX(180deg) translateX(-55%) translateY(50%);
      // clip-path: polygon(0 100%, 50% 0, 100% 100%);
    }
  }

  .topSelectList {
    flex: 1;
    display: flex;
    // align-items: center;
    // justify-content: center;
    flex-wrap: wrap;
    position: relative;
    padding-left: 10px;

    .topSelectItem {
      align-items: center;
      justify-content: center;
      margin: 10px;
      &:first-child {
        margin-left: 80px;
      }
    }
    .selectAll {
      position: absolute;
      color: #606266;
      // background: #e2f3ff;
      font-size: 14px;
      padding: 0;
      width: 50px;
      height: 20px;
      line-height: 20px;
      text-align: center;
      border-radius: 3px;
      cursor: pointer;
    }
    .topSelectMore {
      // width: 60px;
      height: 26px;
      display: flex;
      align-items: center;
      margin: 0 20px;
      position: absolute;
      right: -70px;
      font-size: 14px;
      top: 43px;
      button {
        padding: 4px 8px;
        display: flex;
        justify-content: center;
        align-items: center;
        border: none;
        color: rgba(85, 85, 85, 1);
        border-radius: 4px;
        background: rgba(222, 226, 231, 1);
        cursor: pointer;
        &:focus {
          outline: none;
        }
      }
      i {
        font-size: 5px;
        margin-left: 4px;
      }
    }
  }
}
::v-deep .el-checkbox {
  font-weight: 400;
  .el-checkbox__inner {
    width: 12px;
    height: 12px;
    border-radius: 3px;
    background: transparent;
    border: 1px solid #9ea9c1;
  }
  .el-checkbox__label {
    padding-left: 4px;
  }
  .el-checkbox__input.is-checked .el-checkbox__inner,
  .el-checkbox__input.is-indeterminate .el-checkbox__inner {
    background: transparent;
  }
  .el-checkbox__inner::after {
    left: 3px;
    height: 5px;
    width: 3px;
    border: 1.5px solid #0b6eff;
    border-left: 0;
    border-top: 0;
  }
}
</style>
